package com.pixels;

import java.awt.*;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JFrame;

import com.pixels.graphics.Render;
import com.pixels.input.InputHandler;
import com.Menu.Menu;

public class Display extends Canvas implements Runnable {
	private static final long serialVersionUID = 1L;

	public static final int WIDTH = 800;
	public static final int HEIGHT = 600;
	public static final String TITLE = "Asteroids";

	private Thread thread;
	private Render screen;
	private BufferedImage img;
	private Game game;
	private Menu menu;
	private boolean running = false;
	private int[] pixels;
	private InputHandler input;

	public boolean menuOpen = true;

	public Display() {
		Dimension size = new Dimension(WIDTH, HEIGHT);
		setPreferredSize(size);
		screen = new Render(WIDTH, HEIGHT);
		game = new Game(WIDTH, HEIGHT);
		menu = new Menu(WIDTH, HEIGHT);
		img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
		pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();

		input = new InputHandler();

		addKeyListener(input);
		addFocusListener(input);
		addMouseListener(input);
		addMouseMotionListener(input);
	}

	private void start() {
		if (running)
			return;
		running = true;
		thread = new Thread(this);
		thread.start();
	}

	private void stop() {
		if (!running)
			return;
		running = false;
		try {
			thread.join();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
	}

	private void restart() {
		screen = new Render(WIDTH, HEIGHT);
		game = new Game(WIDTH, HEIGHT);
		img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
		pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
	}

	public void run() {

		final int fps = 30;
		final int sleepTime = 1000 / fps;

		long timeUsed = System.currentTimeMillis();

		while (running) {
			timeUsed = System.currentTimeMillis();
			tick();
			render();
			timeUsed = System.currentTimeMillis() - timeUsed;
			if (timeUsed <sleepTime) {
				try {
					Thread.sleep(sleepTime - timeUsed);
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}

		}
	}

	private void tick() {
		game.tick(input);
		screen.draw(game);
	}

	private void render() {
		BufferStrategy bs = this.getBufferStrategy();

		if (bs == null) {
			createBufferStrategy(3);
			return;
		}

		for (int i = 0; i < WIDTH * HEIGHT; i++) {
			pixels[i] = screen.pixels[i];
		}

		Graphics g = bs.getDrawGraphics();
		g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);

		g.setColor(Color.white);
		g.drawString("Life: " + game.ship.life, 40, 10);
		g.drawString("Score: " + game.ship.score, 40, 20);
		g.drawString("Level: " + game.level, 40, 30);

		if (game.ship.life < 1) {
			restart();
		}

		g.dispose();
		bs.show();
	}

	public static void main(String[] args) {
		Display game = new Display();
		JFrame frame = new JFrame();

		frame.add(game);
		frame.setTitle(TITLE);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.pack();
		frame.setVisible(true);

		game.start();
	}
}
